package com.yunfei.api.v2.service;
import com.yunfei.api.v2.common.YunHeTongApiContant;
import com.yunfei.api.v2.param.LongTokenParam;
import com.yunfei.api.v2.param.heTongDownLoad.HeTongDownLoadParam;
import com.yunfei.bo.result.ResultModel;
import com.yunfei.contants.ApplicationConstant;
import com.yunfei.enums.ResultStatus;
import com.yunfei.utils.DateUtil;
import com.yunfei.utils.http.DownLoadContractHttpUtil;
import com.yunfei.utils.string.StringUtil;
import com.yunfei.yunhetong.elecContBeneAcctDtl.bo.ElecContBeneAcctDtlBo;
import com.yunfei.yunhetong.elecContBeneAcctDtl.service.ElecContBeneAcctDtlService;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;

/**
* @Description: 合同签署报告下载
* @Author: zhengyunfei
* @Date: 2019/8/27
* @time:10:52
*/
@Service
public class DownLoadContractSignReportService {
    @Autowired
    private GetLongTokenService getLongTokenService;
    @Autowired
    private ElecContBeneAcctDtlService elecContBeneAcctDtlService;
    @Autowired
    private Environment env;
    public ResultModel download(HeTongDownLoadParam param){
        ResultModel resultModel=new ResultModel();
        try{
            String token=getLongTokenService.getLongToken(new LongTokenParam());
            //获取合同存放服务器地址
            String savePath=env.getProperty(ApplicationConstant.FILE_IMAGE_PATH);
            String domain=env.getProperty(ApplicationConstant.IMAGE_SERVER_DOMAIN);
            //根据合同id，获取合同编号
            ElecContBeneAcctDtlBo queryParam=new ElecContBeneAcctDtlBo();
            queryParam.setContractId(param.getIdContent());
            ElecContBeneAcctDtlBo bo=elecContBeneAcctDtlService.findByBo(queryParam);
            if(!StringUtil.isNullOrEmpty(bo)){
                String contractNum=bo.getContractNum();//合同编号，也是合同辩题
                String today= DateUtil.getToDay("yyyyMMdd");
                savePath=savePath+YunHeTongApiContant.YUNHETONG_FILE_DIR+"/"+today+"/";
                String fileName=contractNum+"_signReport"+".pdf";
                String url= YunHeTongApiContant.DOWNLOAD_CONTRACT_SIGN_REPORT_API;//合同签署报告下载
                JSONObject jsonObject=JSONObject.fromObject(param);

                String jsonParam=jsonObject.toString();
                JSONObject resultJson=DownLoadContractHttpUtil.post(token,url,jsonParam,savePath,fileName);
                if(!resultJson.get("code").equals("200")){
                    resultModel.setData(resultJson);
                    return resultModel;
                }
                //返回文件访问文件
                String result=domain+"files/"+YunHeTongApiContant.YUNHETONG_FILE_DIR+"/"+today+"/"+fileName;
                resultModel.setData(result);
            }
            resultModel.setCode(ResultStatus.SUCCESS.getCode());
            resultModel.setMessage(ResultStatus.SUCCESS.getMessage());
        }catch (Exception e){
            resultModel.setCode(ResultStatus.ERROR.getCode());
            resultModel.setMessage(ResultStatus.ERROR.getMessage());
            e.printStackTrace();
        }
        return resultModel;
    }


}
